package com.yimi.yinhepda.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.text.TextUtils;

import com.pda.ym.utils.ActivityStack;
import com.pda.ym.utils.Constants;
import com.yimi.yinhepda.utils.CheckNetTimeUtil;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.views.WelcomeActivity;
import com.yimi.yinhepda.views.break_card.BreadCardActivity;
import com.yimi.yinhepda.views.offline.load.OffLineLoadUtil;
import com.yimi.yinhepda.views.offline.unload.OffLineUnLoadUtil;

/**
 * @author 000432
 */
public class BootBroadcastReceiver extends BroadcastReceiver {
    /**
     * 开机广播
     */
    public static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
    /**
     * 日期变化
     */
    public static final String ACTION_DATE_CHANGED = Intent.ACTION_DATE_CHANGED;
    /**
     * 时间变化
     */
    public static final String ACTION_TIME_CHANGED = Intent.ACTION_TIME_CHANGED;
    /**
     * 时区变化
     */
    public static final String ACTION_TIMEZONE_CHANGED = Intent.ACTION_TIMEZONE_CHANGED;

    private Context mContext, mCurrContext;

    /**
     * 重写onReceive方法
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        //开机监听
        if (action.equals(ACTION_BOOT)) {
            Intent intent2;
            //获取设备品牌
            boolean loginPDT = SharedPreferUtil.getInstance().getBooleanValue(Common.PDT_DEV);
            if (loginPDT) {
                //工业级平板设备并且已登录，则跳转至打卡界面
                intent2 = new Intent(context, BreadCardActivity.class);
            } else {
                intent2 = new Intent(context, WelcomeActivity.class);
            }
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }

        //非离线模式下监听
        SharedPreferUtil sharedPreferUtil = SharedPreferUtil.getInstance();
        String mCompanyCode = sharedPreferUtil.getCompanyCode();
        String mEmpCode = sharedPreferUtil.getWorkNum();
        String mPwd = sharedPreferUtil.getPwd();
        Context currentContext = ActivityStack.getInstance().currentActivity();
        //onResume接受到的action
        if (Constants.ACTION_ONRESUME.equals(action)) {
            CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
        } else
            //系统时间变化监听
            if (ACTION_DATE_CHANGED.equals(action)) {
                //日期变化监听
                CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
            } else if (ACTION_TIME_CHANGED.equals(action)) {
                //时间变化监听
                CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
            } else if (ACTION_TIMEZONE_CHANGED.equals(action)) {
                //时区变化监听
                CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
            } else {
                //网络变化监听
                if (TextUtils.isEmpty(mEmpCode) || TextUtils.isEmpty(mPwd) || TextUtils.isEmpty(mCompanyCode)) {
                    return;
                }
                State wifiState;
                State mobileState;
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
                mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
                if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED == mobileState) {
                    // 手机网络连接成功
                    CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
                    OffLineLoadUtil.uploadOfflineLoadData();
                    OffLineUnLoadUtil.uploadOfflineLoadData();
                } else if (wifiState != null && mobileState != null && State.CONNECTED != wifiState && State.CONNECTED != mobileState) {
                    // 手机没有任何的网络
                } else if (wifiState != null && State.CONNECTED == wifiState) {
                    // 无线网络连接成功
                    CheckNetTimeUtil.checkNetworkTime(currentContext, mCompanyCode, mEmpCode, mPwd);
                    OffLineLoadUtil.uploadOfflineLoadData();
                    OffLineUnLoadUtil.uploadOfflineLoadData();
                }
            }
    }
}